package com.zz.inventory.controller;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.io.IOException;
import java.util.Map;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.zz.inventory.domain.ExperimentScenarios;
import com.zz.inventory.service.IExperimentScenariosService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 实验场景库Controller
 * 
 * @author zz
 * @date 2025-07-08
 */
@RestController
@RequestMapping("/scenarios")
public class ExperimentScenariosController extends BaseController
{
    @Autowired
    private IExperimentScenariosService experimentScenariosService;



    /**
     * 修改实验场景状态,上架下架
     */
    @RequiresPermissions("platform:scenarios:edit")
    @Log(title = "实验场景库", businessType = BusinessType.UPDATE)
    @PostMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody Map<String, Object> params)
    {
        Long scenarioId = Long.valueOf(params.get("scenarioId").toString());
        String status = params.get("status").toString();
        return toAjax(experimentScenariosService.changeStatus(scenarioId, status));
    }

    /**
     * 查询实验场景库列表
     */
    @RequiresPermissions("platform:scenarios:list")
    @GetMapping("/list")
    public TableDataInfo list(ExperimentScenarios experimentScenarios)
    {
        startPage();
        List<ExperimentScenarios> list = experimentScenariosService.selectExperimentScenariosList(experimentScenarios);
        return getDataTable(list);
    }

    /**
     * 导出实验场景库列表
     */
    @RequiresPermissions("platform:scenarios:export")
    @Log(title = "实验场景库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExperimentScenarios experimentScenarios)
    {
        List<ExperimentScenarios> list = experimentScenariosService.selectExperimentScenariosList(experimentScenarios);
        ExcelUtil<ExperimentScenarios> util = new ExcelUtil<ExperimentScenarios>(ExperimentScenarios.class);
        util.exportExcel(response, list, "实验场景库数据");
    }

    /**
     * 获取实验场景库详细信息
     */
    @RequiresPermissions("platform:scenarios:query")
    @GetMapping(value = "/{scenarioId}")
    public AjaxResult getInfo(@PathVariable("scenarioId") Long scenarioId)
    {
        return success(experimentScenariosService.selectExperimentScenariosByScenarioId(scenarioId));
    }

    /**
     * 新增实验场景库
     */
    @RequiresPermissions("platform:scenarios:add")
    @Log(title = "实验场景库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExperimentScenarios experimentScenarios)
    {
        return toAjax(experimentScenariosService.insertExperimentScenarios(experimentScenarios));
    }

    /**
     * 修改实验场景库
     */
    @RequiresPermissions("platform:scenarios:edit")
    @Log(title = "实验场景库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExperimentScenarios experimentScenarios)
    {
        return toAjax(experimentScenariosService.updateExperimentScenarios(experimentScenarios));
    }

    /**
     * 删除实验场景库
     */
    @RequiresPermissions("platform:scenarios:remove")
    @Log(title = "实验场景库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{scenarioIds}")
    public AjaxResult remove(@PathVariable Long[] scenarioIds)
    {
        return toAjax(experimentScenariosService.deleteExperimentScenariosByScenarioIds(scenarioIds));
    }


  //  @RequiresPermissions("platform:scenarios:add")
    @Log(title = "实验场景库", businessType = BusinessType.IMPORT)
    @PostMapping("/upload")
    @CrossOrigin(origins = "*")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            if (file.isEmpty()) {
                return error("上传文件不能为空");
            }

            // 创建上传目录（如果不存在）
            Path uploadDir = Paths.get("uploads");
            if (!Files.exists(uploadDir)) {
                Files.createDirectories(uploadDir);
            }

            // 生成唯一文件名
            String fileName = System.currentTimeMillis() + "_" + file.getOriginalFilename();
            Path filePath = uploadDir.resolve(fileName);

            // 保存文件
            Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);

            return success(Map.of(
                    "fileName", fileName,
                    "filePath", "/uploads/" + fileName
            ));
        } catch (IOException e) {
            return error("文件上传失败: " + e.getMessage());
        }
    }
}
